// JavaScript Document


var shineURL = "shinesurvey.py";
var curQuestion = "";

function shine_prefetch() {

	$.mobile.loadPage( "info.html" );
	$.mobile.loadPage( "shine_question.html" );
}

/* List of the questions */
var questionData = {
	student: {
	name: "Student",
	id: "student",
	description: "Tell us about how you were a good student today:"
	},
	
	learning: {
	name: "Learning",
	id: "learning",
	description: "Tell us about how you were good at learning new things today:"
	},
	
	brave: {
	name: "Brave",
	id: "brave",
	description: "Tell us about how you were brave today:"
	},
	
	athletic: {
	name: "Athletic",
	id: "athletic",
	description: "Tell us about how you were athletic today:"
	},
	
	kind: {
	name: "Kind",
	id: "kind",
	description: "Tell us about how you were kind to others today:"
	},
	
	leader: {
	name: "Leader",
	id: "leader",
	description: "Tell us about how you were a leader today:"
	},
	
	funny: {
	name: "Funny",
	id: "funny",
	description: "Tell us about how you were a funny person today:"
	},
	
	forgiving: {
	name: "Forgiving",
	id: "forgiving",
	description: "Tell us about how you were forgiving today:"
	},
	
	building: {
	name: "Building",
	id: "building",
	description: "Tell us about how you were good at building things today:"
	},
	
	creative: {
	name: "Creative",
	id: "creative",
	description: "Tell us about how you were creative today:"
	},
	
	treat: {
	name: "Treat Others",
	id: "treat-other",
	description: "Tell us about how you treated others fairly today:"
	},
	
	listening: {
	name: "Listening",
	id: "listening",
	description: "Tell us about how you were good at listening to others today:"
	},
	
	helpful: {
	name: "Helpful",
	id: "helpful",
	description: "Tell us about how you were helpful to others today:"
	},
	
	trying: {
	name: "Keep Trying",
	id: "keep-trying",
	description: "Tell us about how you kept trying when things were bad today:"
	}
};

function fillQuestion() {
	
	if( questionData[curQuestion]) {
		
		var question = questionData[curQuestion];
		$("#question_title").html(question.name);
		$("#question_id").val(question.description);
		$("#question_description").html(question.description);
		$("#shine_text").val("");
	}
	else {
		$.mobile.changePage("shine.hml");
	}
}

function sendShine() {

	var url = urlThrive + shineURL;
	var args = $('#shine_form').serialize();
	args = args + "&user_name=" + USER_NAME;
	args = args + "&device_id=" + TEST_DEVICE;
	args = args + "&survey_date=" + getTimeStamp();
	
	if(DEBUG_ALERTS) {
		alert(url + "?" + args);
	}
	
	removePopup();
	$.mobile.silentScroll(0);
	showPopup('&nbsp;&nbsp;Sending...&nbsp;&nbsp;');
	
	jQuery.ajax({
		timeout: 8000,
		type: "GET",
		url: url,
		data: args,
		dataType: "text",
		crossDomain: true,

		//success: function (msg) {
		success: function(data, textStatus, jqXHR){

			var message = data.split(":");
			
			if( message.length < 3) {
				$.mobile.silentScroll(0);
				removePopup();
				showPopup('Server error, please try again later.'); 
				return;
			}
			
			var error = parseInt(message[0]);
			
			// success! redirect to login page
			if (error == 0){
				removePopup();
				$.mobile.changePage("../../index.html");	
			}
			
			// error! display message
			else if (error == -1) {
				$.mobile.silentScroll(0);
				removePopup();
				showPopup(message[1]); 
			}
			
			// error! display message and move to form element
			else if (error == -2 ) {
				$.mobile.silentScroll(0);
				removePopup();
				showPopup(message[1]); 
			}
		},
		error: function(jqXHR, textStatus, errorThrown){

			removePopup();
			$.mobile.changePage("../../index.html");
		}
	}); 	
}
